<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!--
 * FCKeditor - The text editor for Internet - http://www.fckeditor.net
 * Copyright (C) 2003-2007 Frederico Caldeira Knabben
 *
 * == BEGIN LICENSE ==
 *
 * Licensed under the terms of any of the following licenses at your
 * choice:
 *
 *  - GNU General Public License Version 2 or later (the "GPL")
 *    http://www.gnu.org/licenses/gpl.html
 *
 *  - GNU Lesser General Public License Version 2.1 or later (the "LGPL")
 *    http://www.gnu.org/licenses/lgpl.html
 *
 *  - Mozilla Public License Version 1.1 or later (the "MPL")
 *    http://www.mozilla.org/MPL/MPL-1.1.html
 *
 * == END LICENSE ==
 *
 * Special tags dialog window.
 * see http://www.mediawiki.org/wiki/Help:Magic_words for documentation
-->
<html>
<head>
	<title>Special Tag Properties</title>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<meta name="robots" content="noindex, nofollow" />
	<script type="text/javascript">

var oEditor		= window.parent.InnerDialogLoaded() ;
var FCK			= oEditor.FCK ;
var FCKLang		= oEditor.FCKLang ;
var FCKConfig	= oEditor.FCKConfig ;
var FCKRegexLib	= oEditor.FCKRegexLib ;
var FCKTools	= oEditor.FCKTools ;

document.write( '<script src="' + FCKConfig.BasePath + 'dialog/common/fck_dialog_common.js" type="text/javascript"><\/script>' ) ;

	</script>
	<script type="text/javascript">

// list with available special tags and variables
function TagNode(name, type, desc) {
    this.name = name;
    this.type = type;
    this.desc = desc;
}

TagNode.prototype.getLabel = function() {
    switch (this.type) {
        case 't' :
            return '<' + this.name + '>'
        case 'c' :
            return '__' + this.name + '__'
        case 'v' :
        case 'w' :
        case 'p' :
            return '{{' + this.name + '}}'
    }
}

TagNode.prototype.getTooltip = function(content) {
    if (!content)
        return this.getLabel();
    content = FCKTools.HTMLDecode(content).replace(/fckLR/g, '\r\n');
    switch (this.type) {
        case 't' :
            return '<' + this.name + '>'
                   + content
                   + '</' + this.name + '>';
        case 'p' :
            return '{{' + this.name + ':'
                   + content + '}}';
    }
}

function TagNodeList() {
    this.list = [];
}

TagNodeList.prototype.add = function(name, type, desc) {
    this.list[this.list.length] = new TagNode(name, type, desc);
}
TagNodeList.prototype.makeSelection = function(oSelect, type, name) {
    var oNewSelect = oSelect.cloneNode(false);
    var tagSelected = false;
    for (var i = 0; i < this.list.length; i++) {
        if (this.list[i].type == type) {
            var oOption = document.createElement('option');
            oOption.value = this.list[i].name;
            if (name == this.list[i].name) {
                oOption.selected = 'selected';
                tagSelected = true;
            }
            oLabel = document.createTextNode(this.list[i].getLabel());
            oOption.appendChild(oLabel);
            oNewSelect.appendChild(oOption);
        }
    }
    // no tag was selected yet
    if (! tagSelected ) {
        // name is set then the selected tag is a custom tag which
        // can't be received by the Ajax call at the moment, therefore
        // add it manually to the list
        if (name) {
            var oOption = document.createElement('option');
            oOption.value = name;
            oOption.selected = 'selected';
            this.add(name, type, '');
            var oLabel = document.createTextNode(this.list[this.list.length -1].getLabel());
            oOption.appendChild(oLabel);
            oNewSelect.appendChild(oOption);
        }
        // select the first tag
        else {
            oNewSelect.getElementsByTagName('option')[0].selected = 'selected';
        }
    }
    
    oSelect.parentNode.insertBefore(oNewSelect, oSelect);
    oSelect.parentNode.removeChild(oSelect);
} 
TagNodeList.prototype.initFromXml = function(xml) {
    try { //Internet Explorer
        xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
        xmlDoc.async="false";
        xmlDoc.loadXML(xml);
    }
    catch(e) {
        parser=new DOMParser();
        xmlDoc=parser.parseFromString(xml,"text/xml");
    }
    if (xmlDoc) {
        var items = xmlDoc.getElementsByTagName('items');
        for (var i = 0; i < items.length; i++) {
            var type = items.item(i).getAttribute('type');
            var item = items.item(i).getElementsByTagName('item');
            for (var j = 0; j < item.length; j++) {
                desc = item.item(j).childNodes[0].nodeValue;
                this.add(item.item(j).getAttribute('name'), type, desc);
            }
        } 
    }
}
TagNodeList.prototype.getDescByName = function(name) {
    for (var i = 0; i < this.list.length; i++) {
        if (this.list[i].name == name)
            return this.list[i].desc
    }
}
TagNodeList.prototype.getTypeByName = function(name) {
    for (var i = 0; i < this.list.length; i++) {
        if (this.list[i].name == name)
            return this.list[i].type
    }
} 

// Get the selected flash embed (if available).
var oFakeImage = FCK.Selection.GetSelectedElement() ;
var oTemplateSpan ;
var sFakeClass = 'FCK__MWSpecial';
var sSelectedTag = "";
var oSpecialTagList;
var sOldParams = "";

if ( oFakeImage )
{
	if ( oFakeImage.tagName == 'IMG' )
	{
		if ( oFakeImage.getAttribute('_fck_mw_special') )
		{
			oTemplateSpan = FCK.GetRealElement( oFakeImage ) ;
			sFakeClass = 'FCK__MWSpecial';
		}
		else if ( oFakeImage.getAttribute('_fck_mw_nowiki') )
		{
			oTemplateSpan = FCK.GetRealElement( oFakeImage ) ;
			sFakeClass = 'FCK__MWNowiki';
		}
		else if ( oFakeImage.getAttribute('_fck_mw_noinclude') )
		{
			oTemplateSpan = FCK.GetRealElement( oFakeImage ) ;
			sFakeClass = 'FCK__MWNoinclude';
		}
		else if ( oFakeImage.getAttribute('_fck_mw_gallery') )
		{
			oTemplateSpan = FCK.GetRealElement( oFakeImage ) ;
			sFakeClass = 'FCK__MWGallery';
		}
		else if ( oFakeImage.getAttribute('_fck_mw_onlyinclude') )
		{
			oTemplateSpan = FCK.GetRealElement( oFakeImage ) ;
			sFakeClass = 'FCK__MWOnlyinclude';
		}
		else if ( oFakeImage.getAttribute('_fck_mw_includeonly') )
		{
			oTemplateSpan = FCK.GetRealElement( oFakeImage ) ;
			sFakeClass = 'FCK__MWIncludeonly';
		}
		else
			oFakeImage = null ;
	}
	else
		oFakeImage = null ;
}

window.onload = function()
{
	// Translate the dialog box texts.
	oEditor.FCKLanguageManager.TranslatePage(document) ;
	
    // Make an Ajax search for the availabe special tags and variables.
	LoadFunctionList();

	// Load the selected link information (if any).
	LoadSelection() ;
	LoadDocumentation();

	// Activate the "OK" button.
	window.parent.SetOkButton( true ) ;
	window.parent.SetAutoSize( true ) ;
}
function MakeSelection(type, name) {
    if (oSpecialTagList) { 
        oSpecialTagList.makeSelection(GetE('xSelectTag'), type, name);
        // reload the correct description for the current selected entry
        GetE('xDefinition').value = oSpecialTagList.getDescByName(GetE('xSelectTag').value);
    }
    // special tags and parser functions can have params. If the readonly attribute
    // is set, remove it
    if ( (type == 'p' || type == 't') && GetAttribute(GetE('xTemplateRaw'), 'readonly') ) {
        SetAttribute(GetE('xTemplateRaw'), 'readonly', null);
        GetE('xTemplateRaw').value = sOldParams;
        GetE('xTemplateRaw').style.fontStyle = 'normal';
    }
    // for constants, datetime and site variables, set the textbox to readonly
    // because they have no parameters
    else if (type == 'c' || type == 'v' || type == 'w') {
        SetAttribute(GetE('xTemplateRaw'), 'readonly', 'readonly');
        sOldParams = GetE('xTemplateRaw').value;
        GetE('xTemplateRaw').value = FCKLang.wikiDlgSpecialNoParamsNeeded || "This tag doesn't need parameters";
        GetE('xTemplateRaw').style.fontStyle = 'italic';
    }
}

function SelectRadioButton(type, name) {
    var types = [ 't', 'c', 'v', 'w', 'p' ];
    for (var t in types) {
        if (types[t] == type) {
            GetE('xSelectType_' + types[t]).checked = true;
            GetE('xSelectType_' + types[t]).parentNode.style.fontWeight = 'bold';
        }
        else
            GetE('xSelectType_' + types[t]).parentNode.style.fontWeight = 'normal';
    }
    // now build the selection
    MakeSelection(type, name);
}

function LoadSelection() {

        // no element is selected, therefore we start with the default
        // selection list
	if ( !oTemplateSpan ) {
            // set the radiobutton and the tag list to the default values
            SelectRadioButton('t');
            return ;
    }
    // we edit an existing special tag, read it's content
	GetE('xTemplateRaw').value = FCKTools.HTMLDecode(oTemplateSpan.innerHTML).replace(/fckLR/g,'\r\n' ).replace( /&quot;/g, '"' ) ;
	var tagName = oTemplateSpan.getAttribute('_fck_mw_tagname');
	var tagType = oTemplateSpan.getAttribute('_fck_mw_tagtype');

    // set the current tag being the selected one in the selection list
	GetE('xSelectTag').value = tagName;
    // depending on the tag type, we need to preselect the radio button
    // build selection based on the current type of the selected tag
    SelectRadioButton(tagType, tagName);
}

function LoadDocumentation(){
	var tagName = GetE('xSelectTag').value;
	GetE('xDefinition').innerHTML = (oSpecialTagList)
            ? oSpecialTagList.getDescByName(tagName)
            : FCKLang.wikiDlgSpecialNoDocuHere || 'No documentation available';
}
function LoadFunctionList() {
    var empty = ''
    oEditor.window.parent.sajax_request_type = 'GET' ;
    oEditor.window.parent.sajax_do_call( 'wfSajaxSearchSpecialTagFCKeditor',[empty], LoadSearchResults ) ;
}
function LoadSearchResults( result ) {
    oSpecialTagList = new TagNodeList();
    oSpecialTagList.initFromXml(result.responseText.Trim());
    LoadSelection();
}

//#### The OK button was hit.
function Ok()
{
	if ( !oTemplateSpan )
	{
		oTemplateSpan = FCK.EditorDocument.createElement( 'SPAN' ) ;
		oTemplateSpan.className = 'fck_mw_special' ;
		SetAttribute( oTemplateSpan, '_fck_mw_customtag', 'true' ) ;
	}
	
	var templateData = FCKTools.HTMLEncode(GetE('xTemplateRaw').value.Trim().replace(/(\r\n|\n)/g, 'fckLR' )).replace( /"/g, '&quot;' ) ;
	
	oTemplateSpan.innerHTML = templateData ;
    var tagName = GetE('xSelectTag').value;
    var tagType = oSpecialTagList.getTypeByName(GetE('xSelectTag').value );
	SetAttribute( oTemplateSpan, '_fck_mw_tagname', tagName ) ;
	SetAttribute( oTemplateSpan, '_fck_mw_tagtype', tagType ) ;
    var tooltip;

	switch (GetE('xSelectTag').value)
	{
		case 'noinclude':
		sFakeClass = 'FCK__MWNoinclude';
		break;
		case 'gallery':
		sFakeClass = 'FCK__MWGallery';
		break;
		case 'nowiki':
		sFakeClass = 'FCK__MWNowiki';
		break;
		case 'includeonly':
		sFakeClass = 'FCK__MWIncludeonly';
		break;
		case 'onlyinclude':
		sFakeClass = 'FCK__MWOnlyinclude';
		break;
		default:
		sFakeClass = 'FCK__MWSpecial';
                var tagNode = new TagNode( tagName, tagType, '' );
                tooltip = tagNode.getTooltip(templateData);
		break;
	}
	
	if ( !oFakeImage )
	{
		oFakeImage	= oEditor.FCKDocumentProcessor_CreateFakeImage( sFakeClass, oTemplateSpan ) ;
		oFakeImage.setAttribute( '_fck_mw_special', 'true', 0 ) ;
                // for Special Tags we add title and alt attribute to the fake image
                if (sFakeClass == 'FCK__MWSpecial') {
                    oFakeImage.setAttribute( 'alt', tooltip);
                    oFakeImage.setAttribute( 'title', tooltip);
                }
		oFakeImage	= FCK.InsertElement( oFakeImage ) ;
	}
	else {
		oFakeImage.className = sFakeClass ;
                // for Special Tags reset the title and alt attribute according to the new element
                if (sFakeClass == 'FCK__MWSpecial') {
                    oFakeImage.setAttribute( 'alt', tooltip);
                    oFakeImage.setAttribute( 'title', tooltip);
                }
                else {
                    if (oFakeImage.getAttribute('alt'))
                        oFakeImage.removeAttribute('alt');
                    if (oFakeImage.getAttribute('title'))
                        oFakeImage.removeAttribute('title');
                }
        }

	return true ;
}

	</script>
    <style type="text/css">
        .label {
            font-weight: bold;
            vertical-align: top;
        }
        .radio {
            white-space: nowrap;
            vertical-align: top;
        }

    </style>
</head>
<body style="overflow: hidden">
		<table cellpadding="0" cellspacing="0" border="0" width="100%">
            <tr>
              <td><span class="label" fcklang="wikiDlgSpecialType">Tag Type:</span></td>
              <td class="radio">
                <input type="radio" name="xSelectType" id="xSelectType_t" value="t" onchange="SelectRadioButton('t')"/>
                <span fcklang="wikiDlgSpecialTags">Special tags</span>
              </td>
              <td class="radio">
                <input type="radio" name="xSelectType" id="xSelectType_c" value="c" onchange="SelectRadioButton('c')"/>
                <span fcklang="wikiDlgSpecialConst">Constants</span>
              </td>
              <td class="radio">
                <input type="radio" name="xSelectType" id="xSelectType_v" value="v" onchange="SelectRadioButton('v')"/>
                <span fcklang="wikiDlgSpecialDatevar">Date/Time variables</span>
              </td>
            </tr>
            <tr>
              <td></td>
              <td class="radio">
                <input type="radio" name="xSelectType" id="xSelectType_w" value="w" onchange="SelectRadioButton('w')"/>
                <span fcklang="wikiDlgSpecialWikivar">Wiki variables</span>
              </td>
              <td class="radio">
                <input type="radio" name="xSelectType" id="xSelectType_p" value="p" onchange="SelectRadioButton('p')"/>
                <span fcklang="wikiDlgSpecialFunc">Parser function hooks</span>
              </td>
              <td></td>
            </tr>
          </table>
          <hr style="border-top: dotted 1px; width: 100%;"/>
          <table cellpadding="0" cellspacing="0" border="0" width="100%">
			<tr>
				<td style="vertical-align: top; padding-right: 5px; border-right: dotted 1px;">
					<span class="label" fcklang="wikiDlgSpecialElement">Tag:</span><br/>
					<select id="xSelectTag" onchange="LoadDocumentation()" size="14">
					</select>
			    </td>
                <td style="width: 100%; height: 100%; padding-left: 5px;">
			     <table cellpadding="0" cellspacing="0" border="0" width="100%" height="100%">
                   <tr> 
			         <td style="vertical-align: top;">
			          <span class="label" fcklang="wikiDlgSpecialDescr">Description:</span><br/>
					  <span id="xDefinition"></span>
				    </td>
				   </tr>
				   <tr>
				     <td style="vertical-align: bottom;">
				       <span class="label" fcklang="wikiDlgSpecialParams">Parameters:</span><br/>
					   <textarea id="xTemplateRaw" style="width: 100%; font-family: Monospace"
						 cols="20" rows="3" wrap="off"></textarea>
				     </td>
				   </tr>
				 </table>
			  </td>
			</tr>
		</table>
</body>
</html>
